package com.semanticweb.project.search.api;

import java.io.File;
import java.io.FileInputStream;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.semanticweb.project.search.config.DocumentConstants;
import com.semanticweb.project.search.config.SearchConstants;

public class SWDocument implements AbstractDocument {

	private String id;
	private String author;
	private String content;
	private String title;
	private String keywords;
	private String summary;

	public SWDocument() {
	}
	
	public SWDocument(String id, String author, String content, String title, String keywords, String summary) {
		this.id = id;
		this.author = author;
		this.content = content;
		this.title = title;
		this.keywords = keywords;
		this.summary = summary;
	}
	
	public SWDocument(String id, String author, File contentFile, String title, String keywords, String summary) {
		this.id = id;
		this.author = author;
		loadContent(contentFile);
		this.title = title;
		this.keywords = keywords;
		this.summary = summary;
	}
	
	public void loadContent(final File contentFile) {
		try {
			final FileInputStream fileInputStream = new FileInputStream(contentFile);
			if (contentFile.getName().endsWith(DocumentConstants.PDF_EXTENSION)) {
				final PDFParser parser = new PDFParser(fileInputStream);
				parser.parse();
				final PDDocument pd = parser.getPDDocument();
				final PDFTextStripper stripper = new PDFTextStripper();
				this.content = stripper.getText(pd);
			} else if (contentFile.getName().endsWith(
					DocumentConstants.DOC_EXTENSION)) {
				final WordExtractor wordExtractor = new WordExtractor(fileInputStream);
				this.content = wordExtractor.getText();
			} else if (contentFile.getName().endsWith(
					DocumentConstants.DOCX_EXTENSION)) {
				final XWPFDocument wordDocument = new XWPFDocument(fileInputStream);
				final XWPFWordExtractor wordExtractor = new XWPFWordExtractor(
						wordDocument);
				this.content = wordExtractor.getText();
			} else {
				this.content = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.content = null;
		}
	}

	@Override
	public Document generateDocument() {
		if (id == null) {
			return null;
		}
		final Document doc = new Document();
		doc.add(new Field(SearchConstants.KEY_ID, id, Field.Store.YES,
				Field.Index.ANALYZED));
		if (author != null)
			doc.add(new Field(DocumentConstants.KEY_AUTHOR, author,
					Field.Store.NO, Field.Index.ANALYZED));
		if (content != null)
			doc.add(new Field(DocumentConstants.KEY_CONTENT, content,
					Field.Store.NO, Field.Index.ANALYZED));
		if (title != null)
			doc.add(new Field(DocumentConstants.KEY_TITLE, title,
					Field.Store.NO, Field.Index.ANALYZED));
		if (keywords != null)
			doc.add(new Field(DocumentConstants.KEY_KEYWORDS, keywords,
					Field.Store.NO, Field.Index.ANALYZED));
		if (summary != null)
			doc.add(new Field(DocumentConstants.KEY_SUMMARY, summary,
					Field.Store.NO, Field.Index.ANALYZED));
		return doc;
	}

	@Override
	public String defaultField() {
		return DocumentConstants.KEY_DEFAULT;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

}
